var vm;

$(function () {
    if(typeof daterangepicker != 'undefined'){
        $('input[id^="filter_input_date_"]').daterangepicker({
            singleDatePicker: true,
            autoUpdateInput:false,
            showDropdowns: true,
            locale: {
                format: 'YYYY-MM-DD'
            }
        }).on('apply.daterangepicker', function(ev, picker) {
            $(this).val(picker.startDate.format('YYYY-MM-DD'));
        });
    }
});
function find(table, query,pageNo) {
    var request = {
        'table': table,
        query: JSON.stringify(query)
    };
    vm.request = request;
    findByPage(pageNo);
}

function findByPage(pageNo) {
    if(pageNo == undefined){
        pageNo = 1;
    }
    vm.loading = true;
    $.post('/dashboard/findTable.html?pageNo='+pageNo, vm.request, function (result) {
        vm.loading = false;
        result = JSON.parse(result);
        vm.header = result.result.header;
        vm.type = result.result.type;
        var page = result.result.page;
        var count = 5;
        var inline = count - (count % 2 +1);
        var activeIndex = 1;
        if(page.pageNo < count || page.pageCount <=count ){
            activeIndex = page.pageNo-1;
        }else{
            if(page.pageNo % inline == 0 && parseInt(page.pageNo/inline) == 0){
                activeIndex = 1;
            }else{
                activeIndex = (page.pageNo - count) % inline +1;
            }
        }
        var _page = {
            current:page.pageNo,
            prev:page.pageNo-1>0?page.pageNo-1:1,
            next:page.pageNo+1<page.pageCount?page.pageNo+1:page.pageCount,
            active:activeIndex,
            count:page.pageCount,
            countSize:page.count,
            array:[1]
        };
        var i = 0;
        var c = 0;
        while (c != count){
            c++;
            var _pageNo = page.pageNo-activeIndex+i;
            if(_pageNo > 0 && _pageNo <= page.pageCount){
                _page.array[i] = _pageNo;
                i++;
            }
        }
        vm.page = _page;
        var indexTitle = [];
        for (var h in vm.header) {
            indexTitle.push(h);
        }

        var data = [];

        for (var i = 0; i < result.result.data.length; i++) {
            var obj = result.result.data[i];
            var _data = [];
            for (var field in obj) {
                var lastStatus = obj['lastStatus'];
                if (lastStatus != undefined) {
                    _data[-2] = lastStatus;
                }
                var index = indexTitle.indexOf(field);
                _data[index] = {val: obj[field], type: result.result.type[field]};
            }
            if (_data.length < indexTitle.length) {
                for (var j = _data.length; j < indexTitle.length; j++) {
                    _data[j] = {val: '', type: 0};
                }
            }
            _data[-1] = obj.id;
            data.push(_data);
        }
        vm.data = data;
    });
}

function tabledata(table,toolbar,params) {
    vm = new Vue({
        el: '#datatable',
        data: {
            search:false,
            loading:true,
            request:{},
            header: {},
            data: [],
            type:[],
            toolbar: toolbar,
            page:{
                current:1,
                prev:1,
                next:1,
                count:1,
                active:1,
                countSize:0,
                array:[1]
            },
            operation:{
                isShow:false,
                index:-1,
                id:''
            }
        },
        methods: {
            tableselect: function (e) {
                var checkbox = $(e.target).closest('.datacell').find('.checkbox1 input')[0];
                checkbox.checked = !checkbox.checked;
            },
            showop:function (e,index) {
                this.operation.isShow = true;
                this.operation.index = index;
                var target = $(e.target).closest('.datacell')[0];
                if(target){
                    this.operation.id = this.data[index][-1];
                }
            },

            hideop:function (e) {
                this.operation.isShow = false;
                this.operation.id = '';
                this.operation.index = -1;
            }
        }
    });
    var query = {};
    var status = vm.toolbar - 2;
    if (status >= 0) {
        query['status'] = status;
    }
    if(params == 'undefined'){
        params = {};
    }
    params = $.extend(query,params);
    find(table, params);
}